// 引入VueRouter
import { createRouter, createWebHistory} from 'vue-router'

// 定义路由规则
const routes = [
  {
    path: '/',
    redirect: '/home' // 重定向
  },
  {
    name: 'home', // 路由名称
    path: '/home', // 路由路径
    redirect: '/home/home',
    component: () => import('@/views/home/index.vue'), // 路由组件
    children: [
      {
        path: 'home',
        component: () => import('@/views/system/home/index.vue')
      },
      {
        path: 'user',
        component: () => import('@/views/system/user/index.vue')
      },
      {
        path: 'goods',
        component: () => import('@/views/shopping/goods/index.vue')
      },
      {
        path: 'order',
        component: () => import('@/views/shopping/order/index.vue')
      }
    ]
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
]

// 配置路由
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_BASE_PATH),
  routes
})

router.beforeEach((to, from, next) => {
  const authorization = sessionStorage.getItem('authorization')
  // 路由拦截
  if (!authorization && to.name !== 'login') {
    next('/login')
  }
  if (authorization && to.name === 'login') {
    next('/')
  }
  next()
})

// 导出路由
export default router